package com.coocaa.x.app.hall.main;

import android.content.Context;
import android.net.Uri;

import com.coocaa.x.framework.utils.AsyncTask;
import com.coocaa.x.uipackage.widget.CoocaaDraweeView;

/**
 * Created by luwei on 15-6-17.
 */
public class SplashView extends CoocaaDraweeView {
    private ShowSplashListener showListener = null;
    private boolean isShow = true;

    public SplashView(Context context, ShowSplashListener listener) {
        super(context);
        this.showListener = listener;
    }

    public interface ShowSplashListener {
        public void beginShow();

        public void endShow();
    }

    public void stopShow() {
        isShow = false;
    }


    public void setImageUri(final Uri defaultUri, final Uri uri, final long defaultTime, final long time) {
        if (defaultUri == null && uri == null)
            throw new RuntimeException("image uri is null");
        if (defaultUri == null)
            setImageURI(uri);
        else
            setImageURI(defaultUri);
        AsyncTask.runOnThread(new Runnable() {
            @Override
            public void run() {
                boolean mIsDefault = uri == null ? true : false;
                long defaultCount = defaultTime / 20;
                if (mIsDefault) {
                    int num = 0;
                    while (true) {
                        num++;
                        if (!isShow || num >= defaultCount) {
                            if (showListener != null)
                                showListener.endShow();
                            break;
                        } else {
                            try {
                                Thread.sleep(20);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                } else {
                    try {
                        Thread.sleep(time);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (showListener != null)
                        showListener.endShow();
                }
            }
        });

    }

}
